/*
 * Bullet.cpp
 *
 *  Created on: 31/10/2011
 */

#include "Bullet.h"
#include "../../common/lib/SDL_rotozoom.h"


Bullet::Bullet(int posX, int posY, Widget *parent, int angle, int duration) :
	WidgetMovimiento(posX, posY,std::string(BULLET_DEFAULT_IMAGE), parent) {

	this->velocity = BULLET_DEFAULT_VELOCITY;
	this->maxDuration = duration;
	this->timeAlive = 0;
	this->angle = angle;
	this->drawable = true;

	//setPosition();
	nextX = parcel->posX;
	nextY = parcel->posY;
}

void Bullet::handleEvent(Event event) {
	//No hace nada por ahora.. capaz màs adelante con las colisiones
}

void Bullet::resolve() {

	if (drawable) {
		//Timer to get seconds between frames
		if (!timer.is_started()) {
			timer.start();
		}

		float secsPerFrame = timer.get_ticks() / 1000.0;
		timeAlive += secsPerFrame;
		if (timeAlive <= maxDuration) {

			int posX0 = parcel->posX;
			int posY0 = parcel->posY;
			nextX = parcel->posX;
			nextY = parcel->posY;

			float angleInRadians = angle * (M_PI / 180);

			float velX = velocity * cos(angleInRadians);
			float velY = (-1.0) * velocity * sin(angleInRadians);

			float deltaX = fabs(velX * secsPerFrame);
			deltaX = floor(deltaX * 100) / 100;
			deltaX = floor(deltaX + 0.5);
			float deltaY = fabs(velY * secsPerFrame);
			deltaY = floor(deltaY * 100) / 100;
			deltaY = floor(deltaY + 0.5);

			if(velX >= 0)
				nextX = posX0 + deltaX;
			else if(velX < 0)
				nextX = posX0 - deltaX;

			if (velY >= 0)
				nextY = posY0 + deltaY;
			else if (velY < 0)
				nextY = posY0 - deltaY;

			if(nextX != 0 || nextY != 0)
			{
				parcel->posX = nextX;
				parcel->posY = nextY;
			}

			timer.start();

		} else {
			this->drawable = false;
			timer.stop();
		}
	}

}

Bullet::~Bullet() {
}

void Bullet::setPosition() {
	int angulo = (int) angle;

	if ((angulo >= 0) && (angulo <= 40)) {
		parcel->posX = parcel->posX + 15;
		parcel->posY = parcel->posY + (-angulo) * 3 / 10;

		std::stringstream ss;
		ss << "x = " << parcel->posX << " y = " << parcel->posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 40) && (angulo <= 130)) {
		parcel->posX = parcel->posX + (-(angulo - 90)) * 3 / 10;
		parcel->posY = parcel->posY - 15;

		std::stringstream ss;
		ss << "x = " << parcel->posX << " y = " << parcel->posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 130) && (angulo <= 180)) {
		parcel->posX = parcel->posX - 15;
		parcel->posY = parcel->posY + (-(angulo - 180)) * 3 / 10;

		std::stringstream ss;
		ss << "x = " << parcel->posX << " y = " << parcel->posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 180) && (angulo <= 220)) {
		parcel->posX = parcel->posX - 15;
		parcel->posY = parcel->posY + (angulo - 180) * 3 / 10;

		std::stringstream ss;
		ss << "x = " << parcel->posX << " y = " << parcel->posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 220) && (angulo <= 310)) {
		parcel->posX = parcel->posX + (angulo - 270) * 3 / 10;
		parcel->posY = parcel->posY + 15;

		std::stringstream ss;
		ss << "x = " << parcel->posX << " y = " << parcel->posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 310) && (angulo <= 360)) {
		parcel->posX = parcel->posX + 15;
		parcel->posY = parcel->posY + (-(angulo - 360)) * 3 / 10;

		std::stringstream ss;
		ss << "x = " << parcel->posX << " y = " << parcel->posY;

		Logger::log("bullet", "setPosition", &ss);
	}
}
